//
//  HeadViewController.m
//  Dear
//
//  Created by n22 on 15/7/16.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "HeadViewController.h"
#import "DetailCell.h"
#import "DetailCellFirst.h"


@interface HeadViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArr;
}
@end

@implementation HeadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitleViewWith:@"关联信息"];
    //获取数据
    [self getData];
    
    //界面
    [self uiConfig];
    
}
- (void)uiConfig {
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, 0,ScreenSize.width , ScreenSize.height - 64)) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = RGBA(246, 246, 246, 1);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
}

- (void)getData
{
    _dataArr = [[NSMutableArray alloc]init];
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpDisplayRelation];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0)
            {
                /*
                 Message = "\U6570\U636e\U52a0\U8f7d\U6210\U529f\Uff01";
                 Result = 0;
                 item =     {
                 "dt_create" = 1437289606000;
                 "dt_lastmod" = 1437289634000;
                 id = "b413204b-d7b4-4173-89bc-8538270d42ff";
                 imei = "CFD9FB25-B84E-489A-9575-F9BFAB8742F0";
                 passWord = 593b354a41944dbe168e0824e37d44979d15b100;
                 salt = 1d8619a657a46952;
                 userCode = 15811111111;
                 userName = junjun;
                 userRoles =         (
                 );
                 */
                NSDictionary *itemDict = dict[@"item"];
                NSString *picture = itemDict[@"picture"];
                NSString *userName = itemDict[@"userCode"];
                NSString *nickName = itemDict[@"userName"];
                NSArray *arrOne = [[NSArray alloc]initWithObjects:picture,userName,nickName, nil];
                NSArray *arrTwo = [[NSArray alloc]initWithObjects:@"", nil];
                [_dataArr addObject:arrOne];
                [_dataArr addObject:arrTwo];
                [_tableView reloadData];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];

}

#pragma mark - 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArr.count;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }else {
        
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        DetailCellFirst * cell = [[[NSBundle mainBundle]loadNibNamed:@"DetailCellFirst" owner:self options:nil]lastObject];
        NSString *picture = _dataArr[indexPath.section][indexPath.row];
        NSArray * arr = @[@"头像",picture];
        cell.moreImageView.hidden = YES;
        [cell showDataWithArray:arr];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
    }else {
        NSArray * declareArr =@[ @[@"",@"昵称",@"账号"],@[@"解除关联"]];
        static NSString * cellID = @"DetailCell";
        DetailCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"DetailCell" owner:self options:nil]lastObject];
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.moreImageView.hidden = YES;
        [cell showDataWithTitle:declareArr[indexPath.section][indexPath.row] detailContent:_dataArr[indexPath.section][indexPath.row]];
        return cell;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section== 0) {
        return 0;
    }else {
        return 44;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        
        UILabel * label = [[UILabel alloc]initWithFrame:(CGRectMake(10, 0, 100, 44))];
        label.text = @"   账号关系";
        label.textAlignment= NSTextAlignmentLeft;
        label.textColor = [UIColor grayColor];
        return label;
    }else {
        return nil;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 60;
    }else {
        return 42;
    }
    
}
#pragma mark -点击cell触发事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"确定要取消关联", nil];
        [sheet showInView:self.view];
    }
}
#pragma mark - sheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId};
        NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpDisassociation];
        [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [DearLoadingView hideHUD];
            if (responseObject) {
                NSDictionary *dict = (NSDictionary *)responseObject;
                if ([dict[@"Result"] integerValue] == 0)
                {
                    [DearLoadingView showTips:dict[@"Message"]];
                    [DearUser sharedInstance].linkFlag = [NSNumber numberWithInt:0];
                    [self performSelector:@selector(backMeDetail) withObject:nil afterDelay:2];
                }else {
                    [DearLoadingView showTips:dict[@"Message"]];
                }
            }else{
                [DearLoadingView showTips:@"获取失败"];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [DearLoadingView hideHUD];
            [DearLoadingView showTips:error];
        }];
    }
}
#pragma mark - 返回
- (void)backMeDetail
{
    [self.navigationController popViewControllerAnimated:YES];
    //断开
    [[NSNotificationCenter defaultCenter]postNotificationName:IsNotLinkDear object:nil];
}

@end
